package com.labuladong.datastructure;

import java.util.NoSuchElementException;

public class MySingleLinkedList<E> {

    //节点定义
    private static class Node<E> {
        E value;
        Node<E> next;

        public Node(E value) {
            this.value = value;
            this.next = null;
        }
    }

    //虚拟头结点
    private Node<E> head;

    //实际尾结点
    private Node<E> tail;

    //链表长度
    private int size;

    //构造方法
    public MySingleLinkedList() {
        this.head = new Node<>(null);
        this.tail = head;
        this.size = 0;
    }

    //在头部添加元素
    public void addFirst(E e) {

        Node<E> newNode = new Node<>(e);
        newNode.next = head.next;
        head.next = newNode;

        if (newNode.next == null) {
            tail = newNode;
        }
        size++;
    }

    //在尾部添加元素
    public void addLast(E e) {
        Node<E> newNode = new Node<>(e);
        tail.next = newNode;
        tail = newNode;
        size++;
    }

    //在指定位置添加元素
    public void add(int index, E e) {
        //检查索引位置是否可以插入
        checkInsertIndexLegality(index);

        if (index == 0) {
            addFirst(e);
        } else if (index == size) {
            addLast(e);
        } else {
            //获取 index 的前一个节点
            Node<E> prevNode = getNode(index - 1);

            //创建新节点
            Node<E> newNode = new Node<>(e);

            //获取 index 的后一个节点
            Node<E> nextNode = prevNode.next;

            // prevNode <-> newNode <-> nextNode
            prevNode.next = newNode;
            newNode.next = nextNode;

            size++;
        }
    }

    //删除第一个结点
    public E removeFirst() {
        //判断链表是否为空
        if (isEmpty()) {
            throw new NoSuchElementException();
        }

        Node<E> firstNode = head.next;
        E deleteData = firstNode.value;
        head.next = firstNode.next;
        firstNode.next = null;

        if (size == 1) {
            tail = head;
        }
        size--;
        return deleteData;
    }

    //删除最后一个结点
    public E removeLast() {
        //判断链表是否为空
        if (isEmpty()) {
            throw new NoSuchElementException();
        }

        //获取最后一个节点
        Node<E> lastNode = tail;
        E deleteData = lastNode.value;

        //获取倒数第二个节点
        Node<E> curNode = head;
        while (curNode.next != lastNode) {
            curNode = curNode.next;
        }
        curNode.next = null;
        tail = curNode;

        size--;
        return deleteData;
    }

    //删除指定位置的结点
    public E remove(int index) {

        //检查索引位置是否合法
        checkIndexLegality(index);

        if (index == 0) {
            return removeFirst();
        } else if (index == size - 1) {
            return removeLast();
        } else {
            //获取 index 的前一个节点
            Node<E> prevNode = getNode(index - 1);

            //获取 index 的后一个节点
            Node<E> nextNode = prevNode.next.next;

            //获取 index 的节点
            Node<E> curNode = prevNode.next;

            E deleteData = curNode.value;
            prevNode.next = nextNode;
            curNode.next = null;

            size--;
            return deleteData;
        }
    }

    //获取第一个结点的值
    public E getFirst() {

        //判断链表是否为空
        if (isEmpty()) {
            throw new NoSuchElementException();
        }
        return head.next.value;
    }

    //获取最后一个结点的值
    public E getLast() {

        //判断链表是否为空
        if (isEmpty()) {
            throw new NoSuchElementException();
        }
        return tail.value;
    }

    //获取指定位置节点的值
    public E get(int index) {

        //检查索引位置是否合法
        checkIndexLegality(index);

        Node<E> curNode = getNode(index);

        return curNode.value;
    }

    //修改指定位置节点的值
    public E set(int index, E e) {

        //检查索引位置是否合法
        checkIndexLegality(index);

        //获取指定位置的节点
        Node<E> curNode = getNode(index);

        //获取旧值
        E oldData = curNode.value;

        //修改值
        curNode.value = e;

        return oldData;
    }

    //工具方法

    //获取链表长度
    public int size() {
        return this.size;
    }

    //判断链表是否为空
    public boolean isEmpty() {
        return this.size == 0;
    }

    // 检查 index 位置是否可以存在元素
    private void checkIndexLegality(int index) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException("Index is illegal!" + ", Size: " + size);
        }
    }

    // 检查 index 位置是否可以插入元素
    private void checkInsertIndexLegality(int index) {
        if (index < 0 || index > size) {
            throw new IndexOutOfBoundsException("Index is illegal!" + ", Size: " + size);
        }
    }

    // 获取 index 位置的节点
    private Node<E> getNode(int index) {
        //检查索引位置是否合法
        checkIndexLegality(index);

        Node<E> curNode = head.next;
        for (int i = 0; i < index; i++) {
            curNode = curNode.next;
        }
        return curNode;
    }

    //展示链表信息
    public void display() {
        System.out.println("size = " + size);
        Node<E> curNode = head.next;
        while (curNode != null) {
            System.out.print(curNode.value + " -> ");
            curNode = curNode.next;
        }
        System.out.println("null");
    }


    public static void main(String[] args) {

        MySingleLinkedList<Integer> list = new MySingleLinkedList<>();

        list.addFirst(1);
        list.addFirst(2);
        list.addLast(3);
        list.addLast(4);
        list.add(2, 5);

        list.display();     // 2 -> 1 -> 5 -> 3 -> 4 -> null

        System.out.println(list.removeFirst());     // 2
        System.out.println(list.removeLast());      // 4
        System.out.println(list.remove(1));  // 5

        list.display();      // 1 -> 3 -> null

        System.out.println(list.getFirst());        // 1
        System.out.println(list.getLast());         // 3
        System.out.println(list.get(1));            // 3
    }
}
